package com.tools.developer.service;

import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;

import com.tools.developer.util.KLog;

public class DeveloperService extends Service {
	static ComponentName top;

	MyHandler mHandler = new MyHandler(this.getApplicationContext());

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		getTopActivity();
	}

	private void getTopActivity() {
		new Thread() {
			@Override
			public void run() {
				while (true) {
					ActivityManager activityMgr = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE);
					top = activityMgr.getRunningTasks(1).get(0).topActivity;
					mHandler.sendEmptyMessage(0);
				}
			}
		}.start();
	}

	static class MyHandler extends Handler {
		Context mContext;

		MyHandler(Context context) {
			mContext = context;
		}

		public void handleMessage(android.os.Message msg) {
			KLog.i("PackageName:" + top.getPackageName());
			KLog.i("ClassName:" + top.getClassName());
			Toast.makeText(mContext, top.getPackageName(), Toast.LENGTH_LONG)
					.show();
		};
	};
}
